$(() => {
    const token = GetQueryString("token");
    //顶部搜索
    $(window).scroll(function() {
        if ($(window).scrollTop() >= 10) {
            $(".inhetop").addClass("on")
        } else {
            $(".inhetop").removeClass("on")
        }
    });

    //搜索
    $(".inhetople").click(function() {
        window.location.href = "view/search.html?token=" + token + '&area_id=0';
    })

    // 首页
    $.ajax({
        type: "post",
        url: url,
        data: { 'method': "Index|index" },
        dataType: 'json',
        success: function(res) {
            // console.log(res);
            if (res.status == 1) {
                let data = res.data;
                let banner = '',
                    news = '',
                    brand = '',
                    recommand_goods = '',
                    plat_area = '';

                // 轮播图
                data.slide.slide_list.forEach(item => {
                    // console.log(item);
                    banner += `
                            <a href="${item.url == '' ? 'javascript:;' : item.url + '&token=' + token}" class="swiper-slide"><img src="${item.img_url == ''?'img/banner.png':item.img_url}" alt="${item.title}" class="baimg" /></a>
                        `;
                });

                // 头条广告
                data.news.forEach(item => {
                    // console.log(item);
                    news += `
                        <li class="infoitem">
                            <a href="fox://headline" class="infolink">${item.title}</a>
                        </li>
                    `;
                })
                // 分类
                data.plat_area.forEach(item => {
                    if (item.id == 1) {
                        plat_area += `
                        <a href="./view/index_stoneZone.html?area_id=${item.id}&token=${token}" class="innalink zone" data-area_id="${item.id}">
                            <img src="${item.bg_img == '' ? 'img/innapic1.png' : item.bg_img}" alt="${item.area_name}" class="innapic" />
                        </a>
                        `;
                    } else {
                        plat_area += `
                        <a href="./view/index_waterHeatingZone.html?area_id=${item.id}&token=${token}" class="innalink zone" data-area_id="${item.id}">
                            <img src="${item.bg_img == '' ? 'img/innapic2.png' : item.bg_img}" alt="${item.area_name}" class="innapic" />
                        </a>
                        `;
                    }
                });

                // 石材、水暖品牌
                data.brand.forEach(items => {
                    brand +=
                        `<div class="intitle_wrap">
                            <div class="intitle">
                                <p class="inti1">${items.brand_name}</p>
                                <a href="view/more_brand.html?area_id=${items.area_id}&token=${token}&recommend_position=1" data-area_id="${items.area_id}" class="inti2 moreBrand">更多</a>
                            </div>
                            <div class="inlinst1">`;
                    items.list.forEach(item => {
                        if (item.company_level >= 1) {
                            brand += `
                            <a href="view/shop_details_company.html?company_id=${item.id}&token=${token}" class="inlist1link">`
                        } else {
                            brand += `
                            <a href="view/shop_details.html?company_id=${item.id}&token=${token}" class="inlist1link">`
                        }
                        brand += `<img src="${item.company_poster_thumb == '' ? 'img/banner.png' : item.company_poster_thumb}" alt="${item.company_name}" class="inlistpic" />
                            </a>
                        `;
                    })
                    brand += `
                        </div>
                    </div>
                    `;
                });
                // 猜你喜欢
                data.recommand_goods.forEach(item => {
                    recommand_goods += `
                    <li class="iz2item">
                        <a href="view/comp_shopDetail.html?id=${item.id}&token=${token}" class="iz2link">
                            <img src="${item.goods_thumb == '' ? 'img/banner.png' : item.goods_thumb}" alt="${item.goods_name}" class="iz2pic" />
                            <span class="iz2title">
                            <p class="iz2ti1">${item.description}</p>`
                    if (item.price_show_model == 1) {
                        recommand_goods += `<p class="iz2ti2">¥${item.goods_price}</p>`
                    } else if (item.price_show_model == 2) {
                        recommand_goods += `<p class="iz2ti2"><del style="color:#666;font-size:.24rem;">¥${item.price_before}</del>/<span style="font-size:.28rem;">¥${item.goods_price}</span></p>`
                    } else {
                        recommand_goods += `<p class="iz2ti2">¥${item.price_area}</p>`
                    }
                    recommand_goods += `</span>
                        </a>
                    </li>`;
                });
                //更多猜你喜欢
                $(document).on('click', '.more_shopLists', function() {
                    location.href = './view/more_goods_A.html?area_id=0&recommend_position=1';
                })

                // 轮播图渲染
                $('.swiper-wrapper').html(banner);
                // 头条广告渲染
                $('.infoList').html(news);
                // 分类渲染
                $('.plat_area').html(plat_area);
                // 品牌专区渲染
                $('.intitle_wrapper').html(brand);
                // 猜你喜欢渲染
                $('.recommand_goods').html(recommand_goods);

                // banner轮播
                if (data.slide.slide_list.length == 1) {
                    new Swiper('.swiper-container', {
                        autoplay: 3000,
                        // pagination: '.swiper-pagination',
                        loop: false
                    })
                } else {
                    new Swiper('.swiper-container', {
                        autoplay: 3000,
                        // pagination: '.swiper-pagination',
                        loop: true
                    })
                }

                // 头条文字轮播
                jQuery(".txtMarquee-top").slide({ mainCell: ".bd ul", autoPage: true, effect: "top", autoPlay: true, vis: 1, delayTime: 300 });
            } else if (res.status == 2) {
                location.href = "fox://logout";
            } else {
                alert(res.message);
            }
        }
    });

    //我的店铺
    $('.myShop').on('click', function() {
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': 'User|user_center', 'token': token },
            dataType: 'json',
            success: function(res) {
                // console.log(res);
                if (res.status == 1) {
                    let data = res.data;

                    if (data.company_confirm == 0) {
                        location.href = 'view/my_shop.html?token=' + token;
                    } else {
                        if (data.company_level >= 1) {
                            location.href = "view/shop_details_company.html?company_id=" + data.company_id + "&token=" + token;
                        } else {
                            location.href = "view/shop_details.html?company_id=" + data.company_id + "&token=" + token;
                        }
                    }
                } else if (res.status == 2) {
                    location.href = "fox://logout";
                } else {
                    alert(res.message);
                }
            }
        })
    })
})